package Lee_code;

import java.util.ArrayList;
import java.util.List;

/**
 * @author blh
 * @date 2024年07月16日 11:53
 */
public class Lee_46_716 {
    int len;
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> permute(int[] nums) {
        len = nums.length;
        int[] seen = new int[len];
        backTrace(new ArrayList<Integer>(), seen, nums);
        return res;
    }

    private void backTrace(ArrayList<Integer> list, int[] seen, int[] nums) {
        if (list.size() == len) {
            res.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < len; i++) {
            if (seen[i] == 1) {
                continue;
            }
            list.add(nums[i]);
            seen[i] = 1;
            backTrace(list, seen, nums);
            list.remove(list.size() - 1);
            seen[i] = 0;
        }
    }
}
